package com.ngovanhau.justsort;

import java.util.List;

import com.ngovanhau.justsort.adapter.LevelAdapter;
import com.ngovanhau.justsort.database.DatabaseHelper;
import com.ngovanhau.justsort.model.Level;
import com.ngovanhau.justsort.model.LevelSetting;
import com.ngovanhau.justsort.sound.MusicBackground;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class LevelActivity extends Activity {

	public MusicBackground music;
	ListView listview;
	List<Level> list;
	LevelAdapter adapter;
	Context context;
	MySharedPreferences mySharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_level);

		this.context = this;
		listview = (ListView) findViewById(R.id.level_list);
		mySharedPreferences = new MySharedPreferences(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		DatabaseHelper db = new DatabaseHelper(this);

		music = new MusicBackground();
		music.loadMusic(this);
		music.play();
		
		list = db.getListHighScore();
		if (list.size() == 0) {
			db.initData();
			list = db.getListHighScore();
		}
		int[] value = { 0, 0, 0, 0, 0, 0};

		// -1: locked, -2: unlock
		value[0] = LevelSetting.unlockType >= 1 ? -1 : -2;
		value[1] = LevelSetting.unlockType >= 6 ? -1 : -2;
		value[2] = LevelSetting.unlockType >= 15 ? -1 : -2;
		value[3] = LevelSetting.unlockType >= 22 ? -1 : -2;
		value[4] = LevelSetting.unlockType >= 25 ? -1 : -2;
		value[5] = LevelSetting.unlockType >= 26 ? -1 : -2;

		list.add(0, new Level(1, 0, 0, 0, value[0]));
		list.add(6, new Level(2, 0, 0, 0, value[1]));
		list.add(16, new Level(3, 0, 0, 0, value[2]));
		list.add(24, new Level(4, 0, 0, 0, value[3]));
		list.add(28, new Level(5, 0, 0, 0, value[4]));
		list.add(30, new Level(6, 0, 0, 0, value[5]));

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				Level level = list.get(position);
				mySharedPreferences.updateCurrentLevel(level.getLevel());
				mySharedPreferences.updateCurrentType(level.getType());
				mySharedPreferences.updateRowLevel(level.getRow());
				mySharedPreferences.updateColumnLevel(level.getColumn());
				LevelSetting.timeMinLevel = level.getTime();
				LevelSetting.TimeOutMoveCurrentLevel = LevelSetting.TimeoutMoveLevelArray[level.getLevel()];
				LevelSetting.TimeOutCurrentLevel = LevelSetting.TimeoutLevelArray[level.getLevel()];
				Intent intent = new Intent(context, MainGameActivity.class);
				startActivity(intent);
			}
		});
		adapter = new LevelAdapter(this, list);
		listview.setAdapter(adapter);
	}
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onStop(){
		super.onStop();
		music.pause();
	}
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			 if(music != null)
				 music.play();
			this.finish();
		}
		return false;
	}

	public void onClick(View view) {
		if (view.getId() == R.id.level_back) {
			this.finish();
		}
	}
}
